package com.woniuxy.charin.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.charin.model.dto.RoleInfoDto;
import com.woniuxy.charin.model.entity.Role;
import com.woniuxy.charin.model.entity.User;
import com.woniuxy.charin.service.RoleService;
import com.woniuxy.charin.service.UserRoleService;
import com.woniuxy.charin.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Slf4j
@Api(tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 获取所有角色
     */
    @GetMapping("/getRoles")
    @ApiOperation("获取所有角色")
    public ResponseEntity<?> list() {
        List<Role> list = roleService.list();
        return ResponseEntity.ok(list);
    }

    /**
     *分页查询角色
     * @param roleInfoDto
     * @return
     */
    @PostMapping("/page")
    @ApiOperation("分页查询用户")
    public ResponseEntity<?> page(@RequestBody RoleInfoDto roleInfoDto) {
        //调用业务层查询
        Page<Role> page = roleService.queryRole(roleInfoDto);
        return ResponseEntity.ok(page);
    }

    /**
     * 添加角色
     */
    @PostMapping("/add")
    @ApiOperation("添加角色")
    public ResponseEntity<?> add(@RequestBody Role role) {
        boolean save = roleService.save(role);
        return ResponseEntity.ok(save);
    }

    /**
     * 编辑角色
     */
    @PutMapping("/edit")
    @ApiOperation("编辑角色")
    public ResponseEntity<?> edit(@RequestBody Role role) {
        boolean update = roleService.updateById(role);
        return ResponseEntity.ok(update);
    }

/**
 * 获取所有拥有部门主管角色的用户
 */
@GetMapping("/getUser")
@ApiOperation("获取所有拥有部门主管角色的用户")
    public ResponseEntity<?>getUser(){
    List<User>users= roleService.getUser();
    return ResponseEntity.ok(users);
}

    /**
     * 批量锁定
     * @param deleteIds
     * @return
     */
    @PostMapping("/batchLock")
    public ResponseEntity<?> batchLock(@RequestBody Integer[] deleteIds) {
        roleService.batchLock(deleteIds);
        return ResponseEntity.ok("批量停用成功");
    }
    /**
     * 批量激活
     * @param deleteIds
     * @return
     */
    @PostMapping("/batchActivate")
    public ResponseEntity<?> batchActivate(@RequestBody Integer[] deleteIds) {
        roleService.batchActivate(deleteIds);
        return ResponseEntity.ok("批量启用成功");
    }



}